<template>
  <div class="addUser">
    <div class="header">添加用户</div>
    <div class="from">
      <el-form :model="addObj"
               :rules="rules"
               ref="ruleForm"
               label-width="100px"
               class="demo-ruleForm">
        <el-form-item label="用户名"
                      prop="username">
          <el-input v-model="addObj.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱"
                      prop="email">
          <el-input v-model="addObj.email"></el-input>
        </el-form-item>
        <el-form-item>
          <span>状态：</span>
          <el-radio v-model="addObj.status"
                    :label="1">正常</el-radio>
          <el-radio v-model="addObj.status"
                    :label="0">禁用</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="addUser">添加</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { addUser } from '@/api'
export default {
  data () {
    return {
      addObj: {
        username: '',
        email: '',
        status: 1
      },
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ]
      },
      radio: '1'
    }
  },
  methods: {
    addUser () {
      this.$refs.ruleForm.validate(vaild => {
        if (vaild) {
          var newObj = JSON.parse(JSON.stringify(this.addObj));
          newObj.status = parseInt(newObj.status);
          // console.log(newObj);
          addUser(newObj).then(res => {
            if (res.code && res.msg === '添加成功') {
              this.$router.push({ name: 'userindex' });
            }
          })
        }
      })
    }
  }
}
</script>

<style lang ='less' scoped>
.addUser {
  padding: 0 30px;
}
.header {
  font-size: 30px;
  font-weight: 900;
  border-bottom: 1px solid #ccc;
  padding: 20px;
  margin-bottom: 50px;
}
.from {
  width: 500px;
  height: 300px;
  /* background-color: red; */
  margin: 0 auto;
}
</style>